package com.tbit.api;

import com.tbit.manager.api.CouponApi;
import com.tbit.manager.api.dto.CouponUserDTO;
import com.tbit.manager.api.dto.PackageDelayDTO;
import com.tbit.uqbike.object.pojo.Coupon;
import com.tbit.uqbike.object.pojo.CouponUser;
import com.tbit.uqbike.object.value.LoginInfo;
import com.tbit.uqbike.service.business.CouponService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 优惠券
 *
 * @author ：zhangtianyou
 * @since 2024/11/22
 */
@RestController
@Slf4j
public class CouponApiImpl implements CouponApi {

    @Autowired
    private CouponService couponService;
    @Override
    public List<CouponUserDTO> getByUserIdPage(Integer userId, Integer accountId, Integer pageSize, Integer pageNum) {
        List<CouponUser> couponUserList = couponService.getByUserIdPage(userId, accountId, pageNum, pageSize);
        List<CouponUserDTO> couponUserDTOList = new ArrayList<>();
        for (CouponUser couponUser : couponUserList) {
            CouponUserDTO couponUserDTO = new CouponUserDTO();
            BeanUtils.copyProperties(couponUser, couponUserDTO);
            couponUserDTOList.add(couponUserDTO);
        }
        return couponUserDTOList;
    }

    @Override
    public List<CouponUserDTO> getCouponIsVail(Integer accountId) {
        List<Coupon> couponList = couponService.getCouponIsVail(accountId);
        List<CouponUserDTO> couponUserDTOList = new ArrayList<>();
        for (Coupon coupon : couponList) {
            CouponUserDTO couponUserDTO = new CouponUserDTO();
            BeanUtils.copyProperties(coupon, couponUserDTO);
            couponUserDTOList.add(couponUserDTO);
        }
        return couponUserDTOList;
    }

    @Override
    public void addBatch(CouponUserDTO couponUserDTO) {
        CouponUser couponUser = new CouponUser();
        BeanUtils.copyProperties(couponUserDTO,couponUser);
        LoginInfo loginInfo = new LoginInfo();
        loginInfo.setId(couponUserDTO.getAccountId());
        loginInfo.setName(couponUserDTO.getLoginUserName());

        couponService.addBatch(couponUser, couponUserDTO.getNum(), couponUserDTO.getEndTime().getTime(),
                couponUserDTO.getSendMessag(),loginInfo);
    }

    @Override
    public void modifyExpireTime(PackageDelayDTO delayDTOApi) {
        com.tbit.uqbike.object.pojo.dto.PackageDelayDTO packageDelayDTOManager =
                new com.tbit.uqbike.object.pojo.dto.PackageDelayDTO();
        BeanUtils.copyProperties(delayDTOApi,packageDelayDTOManager);
        packageDelayDTOManager.setDelayTime(delayDTOApi.getDelayTime().getTime());
        couponService.modifyCouponExpireTime(packageDelayDTOManager);
    }

    @Override
    public void userCouponEdit(Integer couponUserId, Integer userId) {
        couponService.userCouponEdit(couponUserId, userId);
    }
}
